package com.weiiew.backend.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/1/28
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ApiResponse {
    private static final Integer SUCCESS_CODE = 0;
    private static final Integer FAILURE_CODE = 500;
    private int code;
    private String message;
    private Map<String, Object> data = new HashMap<>();

    public ApiResponse(int code, String message) {
        this.setCode(code);
        this.setMessage(message);
    }

    public static ApiResponse ok(String message, String key, Object value) {
        return new ApiResponse(SUCCESS_CODE, message).append(key, value);
    }

    public static ApiResponse ok(String key, Object value) {
        return new ApiResponse(SUCCESS_CODE, "success").append(key, value);
    }

    public static ApiResponse ok(String message) {
        return new ApiResponse(SUCCESS_CODE, message);
    }

    public static ApiResponse ok() {
        return new ApiResponse(SUCCESS_CODE, "");
    }

    public static ApiResponse failure(String message, String key, Object value) {
        return new ApiResponse(FAILURE_CODE, message).append(key, value);
    }

    public static ApiResponse failure(String message, Object value) {
        return new ApiResponse(FAILURE_CODE, message).append("data", value);
    }

    public static ApiResponse failure(String message) {
        return new ApiResponse(FAILURE_CODE, message);
    }

    public static ApiResponse response(boolean isOk, String message) {
        return new ApiResponse(isOk ? SUCCESS_CODE : FAILURE_CODE, message + (isOk ? "成功" : "失败"));
    }

    public ApiResponse append(String key, Object value) {
        this.data.put(key, value);
        return this;
    }
}
